package inabit.inabitcompiler.parser;

import inabit.compiler.parser.SemanticAction;
import inabit.compiler.parser.grammar.NonTerminal;
import inabit.data.InabitMethod;
import inabit.data.Rule;
import inabit.data.Variable;
import inabit.data.expression.Expression;
import inabit.data.expression.arithmetic.ArithmeticExpression;
import inabit.fuzzy.data.FuzzySet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**@author Rafael Rocha*/
public abstract class InabitSemanticAction extends SemanticAction{

	private List<Rule> rules;
	private List<Variable> variablesList;
	private Map<String, InabitMethod> methods;
	
	
	public InabitSemanticAction(NonTerminal nonTerminal) {
		super(nonTerminal);
		rules = new ArrayList<Rule>();
		this.variablesList = new ArrayList<Variable>();
		methods = new HashMap<String, InabitMethod>();
	}
	
	public List<Rule> getDeclaredRules() {
		return rules;
	}
	public void setRules(List<Rule> rules) {
		this.rules = rules;
	}
	public void addRule(Rule r) {
		rules.add(r);
	}
	public void addRules(Collection<Rule> rules) {
		this.rules.addAll(rules);
	}
	public Expression getExpression(String id) {
		return ExpressionMap.getInstance().getExpression(id);
	}
	public FuzzySet getFuzzySet(String id) {
		return FuzzyMap.getInstance().getFuzzySet(id);
	}
	public void addFuzzySet(FuzzySet set) {
		FuzzyMap.getInstance().addFuzzySet(set);
	}
	/**Add the expression to expression base and returns the id of the exp at the base*/
	public String addExpressionAndGetCode(Expression exp) {
		String id = ExpressionMap.getInstance().addExpression(exp);
		return id;
	}
	public void setVariablesList(List<Variable> variablesList) {
		this.variablesList = variablesList;
	}
	public void addVariable(Variable var) {
		this.variablesList.add(var);
	}
	public void addVariables(Collection<Variable> variables) {
		this.variablesList.addAll(variables);
	}
	public List<Variable> getVariablesList() {
		return variablesList;
	}
	public Variable getVariable(String id) {
		for(Variable var : variablesList) {
			if(var.getID().equals(id)) {
				return var;
			}
		}
		return null;
	}
	
	public ArithmeticExpression getArithmeticExpression(String id) {
		return ArithmeticExpressionMap.getInstance().getExpression(id);
	}
	
	public String addArithmeticExpressionAndGetCode(ArithmeticExpression exp) {
		return ArithmeticExpressionMap.getInstance().addExpression(exp);
	}

	public InabitMethod getMethod(String methodName){
		return methods.get(methodName);
	}
	public void setMethods(Map<String, InabitMethod> methods) {
		this.methods = methods;
	}

}
